home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / Kant Pro source Folder / Kant Pro 1.1 ƒ / Shell ƒ / other MSG window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-19  |  6.1 KB  |  251 lines  |  [TEXT/MMCC]

  1. #include "other MSG window.h"
  2. #include "environment.h"
  3. #include "menus.h"
  4. #include "util.h"
  5. #include "file utilities.h"
  6. #include "main.h"
  7. #include "text twiddling.h"
  8. #include "generic window handlers.h"
  9. #include "graphics.h"
  10. #include "window layer.h"
  11. #include "program globals.h"
  12. #include <Folders.h>
  13.  
  14. #define kGrowBoxSize        15
  15. #define PROGRAMS_LIST_NAME    "\pMSG programs list"
  16. #define kListResourceID        151
  17. #define LIST_CREATOR        'ttxt'
  18. #define LIST_TYPE            'ttro'
  19.  
  20. static    Boolean GetListFromDisk(void);
  21. static    Boolean SetupNewListFile(short fileID);
  22. static    Boolean GetListFromResource(void);
  23. static    unsigned long GetModificationDate(FSSpec *theFS);
  24. static    void PutProductListIntoTE(WindowPtr theWindow);
  25.  
  26. static    Handle            gTheList=0L;
  27. static    TEClickLoopUPP    gClickLoopUPP;
  28. static    Boolean            gSetupDone=FALSE;
  29.  
  30. void SetupTheOtherMSGWindow(WindowPtr theWindow)
  31. {
  32.     unsigned char    *titleStr="\pOther MSG products";
  33.     Point            topLeft;
  34.     
  35.     SetWindowHeight(theWindow,
  36.         qd.screenBits.bounds.bottom-qd.screenBits.bounds.top-LMGetMBarHeight()-28);
  37.     SetWindowWidth(theWindow, qd.screenBits.bounds.right-qd.screenBits.bounds.left-70);
  38.     SetWindowType(theWindow, zoomDocProc);
  39.     topLeft.v=qd.screenBits.bounds.top+LMGetMBarHeight()+20;
  40.     topLeft.h=qd.screenBits.bounds.left+10;
  41.     SetWindowTopLeft(theWindow, topLeft);
  42.     SetWindowHasCloseBox(theWindow, TRUE);
  43.     SetWindowMaxDepth(theWindow, 1);
  44.     SetWindowDepth(theWindow, 1);
  45.     SetWindowAutoCenter(theWindow, FALSE);
  46.     SetWindowTitle(theWindow, titleStr);
  47.     SetWindowIsFloat(theWindow, FALSE);
  48.     
  49.     if (gSetupDone)
  50.         return;
  51.     
  52.     if (!GetListFromDisk())
  53.         GetListFromResource();
  54.     
  55.     gClickLoopUPP=NewTEClickLoopProc(MyClikLoop);
  56. }
  57.  
  58. void ShutDownTheOtherMSGWindow(void)
  59. {
  60.     if (gClickLoopUPP!=0L)
  61.         DisposeRoutineDescriptor(gClickLoopUPP);
  62. }
  63.  
  64. void OpenTheOtherMSGWindow(WindowPtr theWindow)
  65. {
  66.     TEHandle        hTE;
  67.     FontInfo        theFontInfo;
  68.     Rect            vScrollBarRect, hScrollBarRect;
  69.     Rect            destRect, viewRect;
  70.     
  71.     hTE=GetWindowTE(theWindow);
  72.     if (hTE==0L)
  73.     {
  74.         SetRect(&vScrollBarRect, GetWindowWidth(theWindow)-kGrowBoxSize, -1,
  75.             GetWindowWidth(theWindow)+1, GetWindowHeight(theWindow)+1-kGrowBoxSize);
  76.         SetRect(&hScrollBarRect, -1, GetWindowHeight(theWindow)-kGrowBoxSize,
  77.             GetWindowWidth(theWindow)-kGrowBoxSize+1, GetWindowHeight(theWindow)+1);
  78.         SetWindowVScrollBar(theWindow,
  79.             NewControl(theWindow, &vScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
  80.         SetWindowHScrollBar(theWindow,
  81.             NewControl(theWindow, &hScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
  82.         
  83.         GetTERect(theWindow, &destRect, TRUE);
  84.         viewRect=destRect;
  85.         hTE=TENew(&destRect, &viewRect);
  86.         SetWindowTE(theWindow, hTE);
  87.         TextFont((**hTE).txFont=36);
  88.         TextSize((**hTE).txSize=12);
  89.         TextFace((**hTE).txFace=0);
  90.         GetFontInfo(&theFontInfo);
  91.         (**hTE).fontAscent=theFontInfo.ascent;
  92.         (**hTE).lineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading;
  93.         AdjustViewRect(hTE);
  94.         TEAutoView(TRUE, hTE);
  95.         (**hTE).clickLoop=gClickLoopUPP;
  96.         PutProductListIntoTE(theWindow);
  97.     }
  98.     
  99.     SetWindowIsActive(theWindow, TRUE);
  100.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  101.     AdjustMenus();
  102. }
  103.  
  104. void DisposeTheOtherMSGWindow(WindowPtr theWindow)
  105. {
  106.     TEHandle        hTE;
  107.     
  108.     hTE=GetWindowTE(theWindow);
  109.     if (hTE!=0L)
  110.     {
  111.         TEDispose(hTE);
  112.         SetWindowTE(theWindow, 0L);
  113.     }
  114. }
  115.  
  116. /* ---------------------------------------------------- */
  117. /* the rest of these are internal to other MSG window.h */
  118.  
  119. static    void PutProductListIntoTE(WindowPtr theWindow)
  120. {
  121.     long            listEOF;
  122.     
  123.     if (gTheList==0L)
  124.         return;
  125.     
  126.     listEOF=GetHandleSize(gTheList);
  127.     HLock(gTheList);
  128.     SetTheText(theWindow, *gTheList, listEOF);
  129.     HUnlock(gTheList);
  130.     TESetSelect(0, 0, GetWindowTE(theWindow));
  131. }
  132.  
  133. static    Boolean GetListFromDisk(void)
  134. {
  135.     Boolean            isNewFile;
  136.     unsigned char    *name=PROGRAMS_LIST_NAME;
  137.     OSErr            isHuman;
  138.     short            vRefNum;
  139.     long            dirID;
  140.     FSSpec            listFileFS, progFS;
  141.     short            listFile;
  142.     long            listEOF;
  143.     unsigned long    diskListModDate, ourListModDate;
  144.     
  145.     isNewFile=FALSE;
  146.     /* find vRefNum and dirID of preferences folder, creating it if necessary */
  147.     isHuman=FindFolder(kOnSystemDisk, 'pref', kCreateFolder, &vRefNum, &dirID);
  148.     
  149.     if (isHuman!=noErr)        /* screwed up already?!? */
  150.         return FALSE;
  151.     
  152.     isHuman=FSMakeFSSpec(vRefNum, dirID, name, &listFileFS);    /* make FSSpec out of it */
  153.     if (isHuman!=noErr)
  154.     {
  155.         if (isHuman==fnfErr)    /* FSSpec is valid, but list file does not exist */
  156.         {
  157.             isHuman=FSpCreate(&listFileFS, LIST_CREATOR, LIST_TYPE, 0);    /* so create it */
  158.             if (isHuman!=noErr)                                        /* or not */
  159.                 return FALSE;
  160.             isNewFile=TRUE;        /* signal that list file is new */
  161.         }
  162.         else return FALSE;
  163.     }
  164.     isHuman=FSpOpenDF(&listFileFS, fsRdWrPerm, &listFile);    /* open list file */
  165.     if (isHuman!=noErr)
  166.         return FALSE;
  167.     
  168.     diskListModDate=GetModificationDate(&listFileFS);
  169.     GetApplicationFSSpec(&progFS);
  170.     ourListModDate=GetModificationDate(&progFS);
  171.     if (diskListModDate<ourListModDate)
  172.         isNewFile=TRUE;
  173.     
  174.     if (isNewFile)
  175.     {
  176.         if (!SetupNewListFile(listFile))
  177.         {
  178.             FSClose(listFile);
  179.             FSpDelete(&listFileFS);
  180.             return FALSE;
  181.         }
  182.         SetModificationDate(&listFileFS, ourListModDate);
  183.     }
  184.     else
  185.     {
  186.         GetEOF(listFile, &listEOF);
  187.         gTheList=NewHandle(listEOF);
  188.         if (gTheList==0L)
  189.         {
  190.             FSClose(listFile);
  191.             FSpDelete(&listFileFS);
  192.             return FALSE;
  193.         }
  194.         HLock(gTheList);
  195.         SetFPos(listFile, 1, 0L);
  196.         if (FSRead(listFile, &listEOF, *gTheList)!=noErr)
  197.         {
  198.             FSClose(listFile);
  199.             FSpDelete(&listFileFS);
  200.             DisposeHandle(gTheList);
  201.             gTheList=0L;
  202.             return FALSE;
  203.         }
  204.         HUnlock(gTheList);
  205.     }
  206.     
  207.     FSClose(listFile);
  208. //    FlushVol(dirID, vRefNum);
  209.     
  210.     return TRUE;
  211. }
  212.  
  213. static    Boolean SetupNewListFile(short fileID)
  214. {
  215.     long            listEOF;
  216.     
  217.     if (!GetListFromResource())
  218.         return FALSE;
  219.     
  220.     listEOF=GetHandleSize(gTheList);
  221.     if (SetEOF(fileID, listEOF)!=noErr)
  222.         return FALSE;
  223.     
  224.     SetFPos(fileID, 1, 0L);
  225.     HLock(gTheList);
  226.     if (FSWrite(fileID, &listEOF, *gTheList)!=noErr)
  227.         return FALSE;
  228.     HUnlock(gTheList);
  229.     
  230.     return TRUE;
  231. }
  232.  
  233. static    Boolean GetListFromResource(void)
  234. {
  235.     gTheList=(Handle)Get1Resource('TEXT', kListResourceID);
  236.     if (gTheList==0L)
  237.         return FALSE;
  238.     if (*gTheList==0L)
  239.         LoadResource(gTheList);
  240.     if (*gTheList==0L)
  241.     {
  242.         DisposeHandle(gTheList);
  243.         gTheList=0L;
  244.         return FALSE;
  245.     }
  246.     
  247.     DetachResource(gTheList);
  248.     
  249.     return TRUE;
  250. }
  251.